package battleship;

import java.io.IOException;
import java.util.Scanner;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import battleship.AI.BasicAI;
import battleship.boat.Boat;
import battleship.data.HighScores;
import battleship.data.Save;
import battleship.data.SaveLoader;
import battleship.data.SaveXML;
import battleship.field.Field;
import battleship.loop.Exit;
import battleship.loop.Saver;
import battleship.loop.Strike;
import battleship.loop.ViewField;
import battleship.player.Player;
import battleship.setup.SetBoats;
import battleship.setup.SetRandom;
import battleship.view.Views;

public class Setup { //dunno if we'll rlly need this
	
	public static final int SETUPPLAYERS = 0;
	public static final int SETUP = 1;
	public static final int LOOP = 2;
	
	private static int state = 0;
	
	public static SaveXML SAVER;
	public static SaveLoader LOADER;

	private static Player[] players = new Player[2];
	private final static int MAXPLAYERS = 2;
	private final static int MINPLAYERS = 0;
	private static String[] positionMap = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", };
	
	public static String[] getPositionMap() {
		return positionMap;
	}
	public static Player[] getPlayers(){
		return players;
	}
	
	public static int getState()
	{
		return Setup.state;
	}
	
	public static void setState(int state)
	{
		Setup.state = state;
	}
	
	public static void loadSetupPlugins()
	{
		PluginSetup[] plugins = new PluginSetup[] {
				new SetBoats(),
				new SetRandom() 
		};
		for(PluginSetup plug : plugins)
		{
			for(Views view : plug.setupSupportedViews())
			{
				if(view == Controller.getGUI()) {
					if(!Controller.getPluginsSetup().contains(plug))
					{
						Controller.getPluginsSetup().add(plug);
					}
				}
			}
		}
		
		//TODO: add support for plugins folder
	}
	
	public static void loadLoopPlugins()
	{
		PluginLoop[] plugins = new PluginLoop[] {
				new Strike(),
				new ViewField(),
				new Saver(),
				new HighScores(),
				new Exit(),
		};
		for(PluginLoop plug : plugins)
		{
			for(Views view : plug.loopSupportedViews())
			{
				if(view == Controller.getGUI()) {
					if(!Controller.getPluginsLoop().contains(plug))
					{
						Controller.getPluginsLoop().add(plug);
					}
				}
			}
		}
		
		//TODO: add support for plugins folder
	}
	
	public static void loadAIPlugins()
	{
		Controller.getPluginsAI().add(new BasicAI());
	}
	
	public static boolean setBoat(Player player, Boat boat, int y, int x, boolean vert)
	{
		return boat.setPosition(player, x, y, vert);		
	}
	
	public static boolean setBoat(Player player, Boat boat, String position) 
	{
		try {
			boolean vert = false;
			String firstPosition;
			char secondPosition[]= new char[position.length() - 2];
			int y;
			int x;
			
			//get vert
			if(position.charAt(position.length() - 1) == 'v') {
				vert = true;
			}
			else if(position.charAt(position.length() - 1) == 'h')
			{
				vert = false;
			}
			else return false;

			firstPosition = String.valueOf(position.charAt(0));
			//get y
			
			int rightOne = 0;
			
			for(int alphabet = 0; alphabet < Setup.getPositionMap().length; alphabet++)
			{
				if(firstPosition.equalsIgnoreCase(Setup.getPositionMap()[alphabet]))
				{
					rightOne = alphabet;
				}
			}
			
			y = rightOne;
			//get x
			for(int length = 0; length < (position.length() - 2); length++)
			{
				secondPosition[length] = position.charAt(length + 1);
			}
			
			x = Integer.parseInt(new String(secondPosition)) - 1;
			return boat.setPosition(player, x, y, vert);
			
			
		} catch (Exception e) // catch fail conversion
		{
			return false;
		}
		
	}
	public static void loadEndPlugins() 
	{
		Controller.getPluginsEnd().add(new HighScores());
		
		//TODO: add support for plugins folder
	}
	
	public static void loadBackgroundPlugins() throws ParserConfigurationException, IOException, SAXException
	{
		Controller.getPluginsBackground().add(new Save());
	}
	
	public static Player getPlayer2(Player player1)
	{
		if (player1 == getPlayers()[0])
		{
			return getPlayers()[1];
		}
		return getPlayers()[0];
	}
		
	
	public static boolean checkFieldParameters(int size){
		
		
		if (size > Field.MAX || size < Field.MIN)
		{
			return false;
		}
		
		return true;
	}
	
	public static boolean goodBoatPos(Player player){
		System.out.println();
		Controller.getController().printBoard(true, player.getField());
		System.out.printf("Is deze boot goed ingesteld? (y/n) ");		//ask if the config is good
		
		boolean goodValue = false;
		
		while(!goodValue){												//keep scanning till theres a good value
			Scanner scanPosYN = new Scanner(System.in);
			
			if(scanPosYN.next().equalsIgnoreCase("y")){					//equals y
				System.out.println();
				goodValue = true;
				return true;
			}
			else if(!scanPosYN.next().equalsIgnoreCase("n")){			//equal anything else but n
				System.out.println();
				System.out.println("U kunt enkel y of nee kiezen");
				goodValue = false;
			}
		}
		return false;
	}
	public static int getMaxplayers() {
		return MAXPLAYERS;
	}
	public static int getMinplayers() {
		return MINPLAYERS;
	}
	
	public static boolean checkNames(String name)
	{
		if(Setup.getPlayers()[0] != null)
		{
			if(name.equalsIgnoreCase(Setup.getPlayers()[0].getName())) return false;
		}
		for(PluginAI plug : Controller.getPluginsAI())
		{
			if (name.equalsIgnoreCase(plug.getName()))
			{
				return false;
			}
		}
		return true;
	}
}
